package com.project.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.music.bean.ListSong;
import com.project.music.service.ListSongService;
import com.project.music.utils.Consts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 歌单歌曲控制类
 */

@Api(tags = "歌单歌曲控制类")
@RestController
@RequestMapping("/listSong")
public class ListSongController {

    @Autowired
    private ListSongService listSongService;

    //向歌单中添加歌曲
    @ApiOperation("向歌单中添加歌曲")
    @PostMapping("/add")
    public JSONObject addListSong(ListSong listSong) {
        JSONObject jsonObject = new JSONObject();
        boolean status = listSongService.save(listSong);

        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "添加成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "添加失败");
        }
        return jsonObject;
    }
    //返回歌单中所有歌曲
    @ApiOperation("返回歌单中所有歌曲")
    @GetMapping("")
    public List<ListSong> allListSong() {
        return listSongService.list();
    }
    //返回指定歌单中所有歌曲
    @ApiOperation("返回指定歌单中所有歌曲")
    @GetMapping("/detail")
    public List<ListSong> listSongOfSongListId(@RequestParam("songListId") @ApiParam("歌单歌曲ID") int id) {
        QueryWrapper<ListSong> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(ListSong::getSongListId, id);
        return listSongService.list(queryWrapper);
    }
    //删除歌单里的歌曲
    @GetMapping("/delete")
    @ApiOperation("删除歌单里的歌曲")
    public Boolean deleteListSong(@RequestParam("songId") @ApiParam("歌曲ID") int songId) {
        QueryWrapper<ListSong> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(ListSong::getSongId, songId);
        return listSongService.remove(queryWrapper);
    }
    //更新歌单中的歌曲信息
    @PostMapping("/update")
    @ApiOperation("更新歌单中的歌曲信息")
    public JSONObject updateListSongMsg(ListSong listSong) {
        JSONObject jsonObject = new JSONObject();
        boolean status = listSongService.updateById(listSong);

        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }
        return jsonObject;
    }
}
